/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 *
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.core.command.processor;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openinsula.vulcano.core.command.Command;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionAttributeSource;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.ReflectionUtils;

public class TransactionalCommandProcessor implements CommandProcessor {

	protected final Log logger = LogFactory.getLog(getClass());

	/**
	 * The {@link TransactionAttributeSource} used to get
	 * {@link TransactionDefinition} of invoked Commands.
	 */
	private final TransactionAttributeSource transactionAttributeSource = new AnnotationTransactionAttributeSource(true);

	private PlatformTransactionManager transactionManager;

	@SuppressWarnings("unchecked")
	@Override
	public <T> T doExecute(final Command<T> command, final CommandChain commandChain) {
		TransactionDefinition transactionDefinition = getTransactionDefinition(command);

		TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager, transactionDefinition);

		return (T) transactionTemplate.execute(new TransactionCallback() {
			@Override
			public Object doInTransaction(final TransactionStatus status) {
				return commandChain.doChain(command);
			}
		});
	}

	protected TransactionDefinition getTransactionDefinition(final Command<?> command) {
		Method executeMethod = ReflectionUtils.findMethod(command.getClass(), "execute");
		TransactionDefinition transactionDefinition = transactionAttributeSource.getTransactionAttribute(executeMethod, command.getClass());

		if (transactionDefinition == null) {
			if (logger.isDebugEnabled()) {
				logger.debug(String.format("No transaction definition found for %s. Using default.", command.getClass()
						.getName()));
			}
			
			transactionDefinition = new DefaultTransactionDefinition();
		}
		
		return transactionDefinition;
	}

	@Autowired
	public void setTransactionManager(PlatformTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}

}
